package com.xyd.wechat.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyd.wechat.common.Constant;
import com.xyd.wechat.service.AutoReplyService;
import com.xyd.wechat.vo.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;

/**
 * 聊天机器人接口：H5，APP，PC
 */
@RestController
@Slf4j
public class ChatController {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private AutoReplyService autoReplyService;

    /**
     * @param message
     * @return
     */
    @RequestMapping(Constant.BASE_PATH + "/chat/robot/api")
    @ResponseBody
    public String api(@RequestBody Message message) throws Exception {
        String fromUserName = message.getFromUserName();
        message.setFromUserName(message.getToUserName());
        message.setToUserName(fromUserName);
        message.setCreateTime(Calendar.getInstance().getTimeInMillis());
        Message msg = autoReplyService.autoReplyByRobot(message);
        return objectMapper.writeValueAsString(msg);
    }
}
